//
//  ScrollViewNSTimer.m
//  定时滚动图片的封装
//
//  Created by dlios on 15-2-1.
//  Copyright (c) 2015年 吴品泽. All rights reserved.
//

#import "ScrollViewNSTimer.h"
#import "NetHandler.h"
@implementation ScrollViewNSTimer


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.selfArray = [NSMutableArray array];
    }
    return self;
}



- (void)creatScroviewNstimer:(NSMutableArray *)array
{    _flag = 0;
    self.selfArray = array;
    //创建滚动视图并设置可滚动范围
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * array.count, 0);
    //self.scrollView.backgroundColor = [UIColor yellowColor];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    //创建pageControl
    self.pageView = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 10, self.frame.size.width,  10)];
    //设置点的个数
    if (array.count > 1) {
        self.pageView.numberOfPages = array.count;
    }
    
    //设置当前点的颜色
    self.pageView.currentPageIndicatorTintColor = [UIColor greenColor];
    //设置所有点得颜色
    self.pageView.pageIndicatorTintColor = [UIColor whiteColor];
    
    [self.pageView addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventTouchUpInside];
    
    for (NSInteger i = 0; i < array.count; i++) {
        NSString *str = [array objectAtIndex:i];
        [NetHandler getDataWithUrl:str completion:^(NSData *data) {
            UIImage *image = [UIImage imageWithData:data];
            self.button = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width *i, 0, self.bounds.size.width, self.bounds.size.height)];
            self.button.tag = i + 100;
            
            [self.button setImage:[UIImage imageNamed:@"music.jpg"] forState:UIControlStateNormal];
            [self.button setImage:image forState:UIControlStateNormal];
            self.button.backgroundColor = [UIColor redColor];
            [self.scrollView addSubview:self.button];
            
            [self.button addTarget:self.timerTarget action:@selector(buttonCliked:) forControlEvents:UIControlEventTouchUpInside];
        }];
    }
    self.scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ScrollViewBack.jpg"]];
    self.userInteractionEnabled = YES;
    self.scrollView.userInteractionEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
   // self.scrollView.showsVerticalScrollIndicator = YES;
    [self addSubview:self.scrollView];
    [self addSubview:self.pageView];
    
    
    [self changeContentoffset];
    
  
   
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger currentPage = scrollView.contentOffset.x / self.frame.size.width;
    self.pageView.currentPage = currentPage;
    
}

- (void)pageChange:(UIPageControl *)page
{
    NSInteger number = page.currentPage;
    self.scrollView.contentOffset = CGPointMake(self.frame.size.width * number, 0);
}


- (void)changeContentoffset
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeContentoffsets) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];
}


- (void)changeContentoffsets
{
    if (self.selfArray.count != 0) {
        self.scrollView.contentOffset  = CGPointMake(self.scrollView.contentOffset.x + self.scrollView.frame.size.width * _flag , 0);
        _flag++;
        if (_flag == 2) {
            _flag = 1;
        }

    }
    
    if (_scrollView.contentOffset.x == _scrollView.frame.size.width * self.selfArray.count ) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }
   
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
